﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.Approval;
using B.S.BasicData.Api.Read.DTOS;
using B.S.BasicData.Domain.Entity.Approval;
using B.S.BasicData.Domian.Entitys;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.Approval
{
    public class GetReportDetailHandler : IRequestHandler<GetReportDetailCommand, ApiPaging<ReportDetailDto>>
    {
        private readonly IBaseRepository<UsersModel> _userRepository;
        private readonly IBaseRepository<ApprovalModel> _approvalRepository;
        private readonly IBaseRepository<DeparmentModel> _deparmentRepository;
        private readonly IBaseRepository<LeaveTypeModel> _leaveTypeRepository;
        private readonly IMapper _mapper;

        public GetReportDetailHandler(IBaseRepository<UsersModel> userRepository, IBaseRepository<ApprovalModel> approvalRepository,
            IMapper mapper, IBaseRepository<LeaveTypeModel> leaveTypeRepository, IBaseRepository<DeparmentModel> deparmentRepository)
        {
            _userRepository = userRepository;
            _approvalRepository = approvalRepository;
            _mapper = mapper;
            _leaveTypeRepository = leaveTypeRepository;
            _deparmentRepository = deparmentRepository;
        }
        /// <summary>
        /// 获取工时报表详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiPaging<ReportDetailDto>> Handle(GetReportDetailCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<ReportDetailDto> result = new ApiPaging<ReportDetailDto>();

            var userid =_userRepository.GetAll().Where(p=>p.UserName==request.UserName).FirstOrDefault();

            var approval = _approvalRepository.GetAll().Where(p => p.UserId == userid.Id && p.StartTime.Month == request.Month);
            var lavetypelist = _leaveTypeRepository.GetAll();
            var deparmentlist = _deparmentRepository.GetAll();
            var userlist = _userRepository.GetAll();
            var query = from a in approval
                        join u in userlist on a.UserId equals u.Id
                        join d in deparmentlist on a.DeparmentId equals d.Id
                        join l in lavetypelist on a.LeaveType equals l.Id
                        select new ReportDetailDto
                        {
                            Id = a.Id,
                            UserId = a.UserId,
                            UserName = u.UserName,
                            DeparmentId = a.DeparmentId,
                            DeparmentName = d.DeparmentName,
                            LeaveType = a.LeaveType,
                            LeaveTypeName = l.LeaveName,
                            Status = a.Status,
                            Reason = a.Reason,
                            StartTime = a.StartTime,
                            YeNo = a.YeNo,
                            EndTime = a.EndTime,
                            Hours = a.Hours,
                            Approvaler = a.Approvaler,
                            Remark = a.Remark,
                        };

            result.Code = StatusEnum.OK;
            result.PageData = query.ToList();
            result.Message = "获取成功";
            return Task.FromResult(result);
        }
    }
}
